package com.code;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;
import com.object.obXuatVaTon;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TK_XuatVaTon extends Activity {
	private String strNhomChon = "";
	private String strKhoChon = "";
	ArrayList<Integer> date_actual;
	private String date;
	private LinearLayout llMainXuatVaTon;
	private TableRow rowFocus;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.xuatvaton);

		llMainXuatVaTon = (LinearLayout) findViewById(R.id.llMainXuatVaTon);
		strNhomChon = super.getIntent().getExtras().getString("strNhomChon");
		strKhoChon = super.getIntent().getExtras().getString("strKhoChon");
		date_actual = super.getIntent().getExtras()
				.getIntegerArrayList("date_tk_xuatton");
		date = String.valueOf(date_actual.get(2))
				+ DetailCuocGoi.setMonth(date_actual.get(1))
				+ Global.formatDay(date_actual.get(0));

		loadData();
	}

	public void loadData() {

		Integer totaSLTon = 0;
		Integer totalsoLuongXuat = 0;

		StringBuffer sql = new StringBuffer(" spMobileNhapXuatChuyenTon '"
				+ date + "', '" + date + "','" + strNhomChon + "','"
				+ strKhoChon + "' ");
		ResultSet rs = Global.excuteQuerySQLServer(sql.toString());

		final TableLayout table = new TableLayout(this);
		llMainXuatVaTon.addView(table);
		TableRow trHead = new TableRow(this);
		trHead.setWeightSum(5.0f);

		TextView tvMaHang = new TextView(this);
		tvMaHang.setText("Mã hàng");
		TK_TongMatHang.setLayoutForTextView(tvMaHang, true, Gravity.CENTER, 10,
				3.0f);

		TextView tvSLXuat = new TextView(this);
		tvSLXuat.setText("SL Xuất");
		TK_TongMatHang.setLayoutForTextView(tvSLXuat, true, Gravity.CENTER, 10,
				1.0f);

		TextView tvSLTon = new TextView(this);
		tvSLTon.setText("SL Tồn");
		TK_TongMatHang.setLayoutForTextView(tvSLTon, true, Gravity.CENTER, 10,
				1.0f,true);

		trHead.addView(tvMaHang);
		trHead.addView(tvSLXuat);
		trHead.addView(tvSLTon);
		table.addView(trHead);
		try {
			while (rs.next()) {
				// set value for TongMatHang
				final obXuatVaTon obXVT = new obXuatVaTon();
				if (rs.getString("Product id") != null)
					obXVT.setProduct_ID(rs.getString("Product id"));
				if (rs.getInt("Xuat") != 0)
					obXVT.setSlXuat(String.valueOf(rs.getInt("Xuat")));
				else
					obXVT.setSlXuat("0");
				if (rs.getInt("Ton") != 0)
					obXVT.setSlTon(String.valueOf(rs.getInt("Ton")));
				else
					obXVT.setSlTon("0");

				// add date in textview
				final TableRow trHeads = new TableRow(this);
				trHeads.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						try {
							if (rowFocus != null)
								rowFocus.setBackgroundColor(Color.TRANSPARENT);
							rowFocus = (TableRow) v;
							v.setBackgroundColor(Color.YELLOW);
						} catch (Exception e) {
						}
					}
				});
				trHeads.setOnLongClickListener(new OnLongClickListener() {
					public boolean onLongClick(View v) {
						// if (!obTMH.getMaHang().equals("")) {
						// Intent i = new Intent(TK_TongLoiNhuan.this,
						// DetailTongMatHang.class);
						// Bundle b = new Bundle();
						// b.putString("strNhomChonDetail", strNhomChon);
						// b.putString("strKhoChonDetail", strKhoChon);
						// b.putString("maHangDetail", obTMH.getMaHang());
						// b.putIntegerArrayList("dateDetail", date_actual);
						// i.putExtras(b);
						// startActivityForResult(i, REQUESTCODE_DETAIL);
						// }
						return false;
					}
				});
				trHeads.setWeightSum(5.0f);

				TextView tvMaHangs = new TextView(this);
				tvMaHangs.setText(obXVT.getProduct_ID());
				TK_TongMatHang.setLayoutForTextView(tvMaHangs, false,
						Gravity.LEFT, 10, 3.0f);

				TextView tvSLXuats = new TextView(this);
				tvSLXuats.setText(obXVT.getSlXuat());
				TK_TongMatHang.setLayoutForTextView(tvSLXuats, false,
						Gravity.CENTER, 10, 1.0f);

				TextView tvSLTons = new TextView(this);
				tvSLTons.setText(obXVT.getSlTon());
				TK_TongMatHang.setLayoutForTextView(tvSLTons, false,
						Gravity.CENTER, 10, 1.0f,true);

				trHeads.addView(tvMaHangs);
				trHeads.addView(tvSLXuats);
				trHeads.addView(tvSLTons);
				table.addView(trHeads);

				totalsoLuongXuat = totalsoLuongXuat+ Integer.valueOf(obXVT.getSlXuat());
				totaSLTon = totaSLTon + Integer.valueOf(obXVT.getSlTon());
				;
				// totalThanhTien = totalThanhTien
				// + Float.valueOf(Float.valueOf(obXVT.getThanhTien()));
				// totalVon = totalVon
				// + Float.valueOf(Float.valueOf(obTMH.getVon()));
				// totalLai = totalLai
				// + Float.valueOf(Float.valueOf(obTMH.getLai()));
			}
			// add date in textview
			TableRow trHeadEnd = new TableRow(this);
			trHeadEnd.setWeightSum(5.0f);

			TextView tvMaHangEnd = new TextView(this);
			tvMaHangEnd.setText("Tổng");
			TK_TongMatHang.setLayoutForTextView(tvMaHangEnd, false,
					Gravity.CENTER, 10, 3.0f);
			tvMaHangEnd.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvSLXuatEnd = new TextView(this);
			tvSLXuatEnd.setText(totalsoLuongXuat.toString());
			TK_TongMatHang.setLayoutForTextView(tvSLXuatEnd, false,
					Gravity.CENTER, 10, 1.0f);
			tvSLXuatEnd.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvSLTonEnd = new TextView(this);
			tvSLTonEnd.setText(totaSLTon.toString());
			TK_TongMatHang.setLayoutForTextView(tvSLTonEnd, false,
					Gravity.CENTER, 10, 1.0f,true);
			tvSLTonEnd.setBackgroundResource(R.layout.border);
//			tvSLTonEnd.setGravity(Gravity.CENTER);

			trHeadEnd.addView(tvMaHangEnd);
			trHeadEnd.addView(tvSLXuatEnd);
			trHeadEnd.addView(tvSLTonEnd);
			table.addView(trHeadEnd);

			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}
}
